#include "tcp.h"
#include "Cola.h"
#include "log.h"
#include <string>
#include <unistd.h>

#define PORT_RECEPTOR_TCP 12345

int main(int argc, char* argv[]){

    int socket = tcpoppas(PORT_RECEPTOR_TCP);
    if(socket <= 0){
	log(stdout, "No se pudo abrir el socket del server receptor.\n");
	exit(-1);
    }

    while(true){
	int newfd = aceptar(socket);
	log(stdout, "Nuevo receptor aceptado.\n");
	if(newfd <= 0){
	    perror("Error al aceptar el socket");
	    continue;
	}

	if(fork() == 0){
	    log(stdout, "Creo un nuevo receptor.\n");
	    char socket[20];
	    sprintf(socket, "%d", newfd);
	    execlp("./receptor", "receptor", socket, NULL);
	    perror("Error al lanzar el receptor");
	}
	close(newfd);
    }

    return 0;
}




